// PauseScreenMenu.java
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class PauseScreenMenu extends StackPane {  // 改为继承StackPane
    private Image continueImage;
    private Image continuePressedImage;
    private Image backToMainImage;
    private Image backToMainPressedImage;
    private Button continueButton;
    private Button backToMainButton;
    private GameEngine gameEngine;

    public PauseScreenMenu(GameEngine gameEngine) {
        this.gameEngine = gameEngine;

        // 创建半透明背景覆盖层
        Rectangle bgOverlay = new Rectangle(
                GameConstants.getScreenWidth(),
                GameConstants.getScreenHeight()
        );
        bgOverlay.setFill(new Color(0, 0, 0, 0.6));

        // 创建按钮容器
        VBox buttonBox = new VBox(30);
        buttonBox.setAlignment(Pos.CENTER);

        // 加载图片资源
        try {
            continueImage = new Image(getClass().getResourceAsStream("/images/continue.png"));
            continuePressedImage = new Image(getClass().getResourceAsStream("/images/continuePressed.png"));
            backToMainImage = new Image(getClass().getResourceAsStream("/images/backtomain.png"));
            backToMainPressedImage = new Image(getClass().getResourceAsStream("/images/backtomainPressed.png"));
        } catch (Exception e) {
            System.err.println("加载暂停菜单图像失败: " + e.getMessage());
        }

        // 创建继续游戏按钮
        continueButton = new Button();
        continueButton.setGraphic(new ImageView(continueImage));
        styleButton(continueButton, continueImage, continuePressedImage);
        continueButton.setOnAction(e -> gameEngine.togglePause());

        // 创建返回主菜单按钮
        backToMainButton = new Button();
        backToMainButton.setGraphic(new ImageView(backToMainImage));
        styleButton(backToMainButton, backToMainImage, backToMainPressedImage);
        backToMainButton.setOnAction(e -> {
            // 重置游戏状态
            gameEngine.resetGame();
            gameEngine.togglePause();
            GameConstants.GAME_PAUSED = false;
            MusicManager.stopGameMusic();
            // 获取当前场景的舞台
            Stage primaryStage = (Stage) getScene().getWindow();

            // 重新创建主菜单场景
            Main main = new Main();
            try {
                main.start(primaryStage); // 完全重新初始化游戏
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });

        buttonBox.getChildren().addAll(continueButton, backToMainButton);

        // 将背景和按钮容器添加到StackPane
        getChildren().addAll(bgOverlay, buttonBox);
    }

    private void styleButton(Button button, Image normalImage, Image hoverImage) {
        button.setStyle("-fx-background-color: transparent;");
        button.setOnMouseEntered(e -> ((ImageView)button.getGraphic()).setImage(hoverImage));
        button.setOnMouseExited(e -> ((ImageView)button.getGraphic()).setImage(normalImage));

        // 添加按下效果
        button.setOnMousePressed(e -> button.setTranslateY(2));
        button.setOnMouseReleased(e -> button.setTranslateY(0));
    }
}